package com.online_exam.service.impl;

import com.online_exam.domain.Message;
import com.online_exam.domain.MessageUser;
import com.online_exam.domain.User;
import com.online_exam.repository.MessageMapper;
import com.online_exam.repository.UserMapper;
import com.online_exam.service.MessageService;

import com.online_exam.viewmodel.admin.message.MessagePageRequestVM;
import com.online_exam.viewmodel.admin.message.MessageSendVM;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    private MessageMapper messageMapper;
    @Resource
    private UserMapper userMapper;


    @Override
    public int insertMessage(Message message) {
        return messageMapper.insertMessage(message);
    }

    @Override
    public int insertMessageUser(MessageUser messageUser) {
        return messageMapper.insertMessageUser(messageUser);
    }
    @Transactional
    @Override
    public int insertMessageByVM(MessageSendVM messageSendVM, Integer userid) {
        User user = userMapper.findByUserId(userid);
//        if (user==null){//这里考虑到已经是登录进来的用户，一是合法用户，故不用验证用户的合法性
//            return 0;
//        }

        Date createDate = new Date();
        Message message = new Message();

        message.setTitle(messageSendVM.getTitle());
        message.setContent(messageSendVM.getContent());
        message.setCreateTime(createDate);
        message.setSendUserId(user.getId());
        message.setSendUserName(user.getUserName());
        message.setSendRealName(user.getRealName());
        message.setReceiveUserCount(messageSendVM.getReceiveUserIds().size());
        message.setReadCount(0);
        messageMapper.insertMessage(message);
        Integer messageId = message.getId();
        Long count=messageSendVM.getReceiveUserIds().stream().map((id->{
            User msguser = userMapper.findByUserId(id);
            MessageUser messageUser = new MessageUser();
            messageUser.setCreateTime(createDate);
            messageUser.setMessageId(messageId);
            messageUser.setReaded(false);
            messageUser.setReceiveUserId(id);
            messageUser.setReceiveRealName(msguser.getRealName());
            messageUser.setReceiveUserName(msguser.getUserName());
            return messageMapper.insertMessageUser(messageUser);

        })).count();
//        for(Integer id : messageSendVM.getReceiveUserIds()){
//            User msguser = userMapper.findByUserId(id);
//            MessageUser messageUser = new MessageUser();
//            messageUser.setCreateTime(createDate);
//            messageUser.setMessageId(message.getId());
//            messageUser.setReaded(false);
//            messageUser.setReceiveUserId(id);
//            messageUser.setReceiveRealName(msguser.getRealName());
//            messageUser.setReceiveUserName(msguser.getUserName());
//            count+=messageMapper.insertMessageUser(messageUser);
//        }

        return count==messageSendVM.getReceiveUserIds().size()?1:0;
    }

    @Override
    public List<Message> select(MessagePageRequestVM vm) {
        return messageMapper.selectByVm(vm);
    }

    @Override
    public List<MessageUser> selectByMsgId(List<Integer> ids) {

        return messageMapper.selectByMsgId(ids);

    }

    @Override
    public List<MessageUser> selectByUserId(Integer id) {
        return messageMapper.selectByUserId(id);
    }

    @Override
    public List<Message> selectByUserMsgId(List<Integer> ids) {
        return messageMapper.selectByUserMsgId(ids);
    }

    /**
     * 当用户读取信息时候，信息变为已读状态，然后获得已读时间，同时发信息已读人数+1
     * @param id
     * @return
     */
    @Override
    @Transactional
    public void readByUser(Integer id) {
        MessageUser messageUser = messageMapper.selectById(id);
        if (messageUser.getReaded()){
            return ;
        }
        Date date = new Date();
        messageUser.setReaded(true);
        messageUser.setReadTime(date);
        messageMapper.ReadMessageUser(messageUser);
        messageMapper.readAdd(messageUser.getId());
        return ;
    }

    @Override
    public int unReadCount(Integer userId) {
        return messageMapper.findUnReadCount(userId);
    }
}
